dofile(arg[1])
require("lfs")

print("----")
print(KEPLER_LUA)
print("----")

KEPLER_DIR   = arg[2]
WIKIDATA_DIR = arg[3]
BASE_URL     = arg[4]

CONFIG_FILE_NAME = "sputnik/config.lua"
STORAGE_MODULE   = nil -- "wikisvnstorage" 




function install_all()

   print("2.1 Create the initial wiki-data directory")
   print("    (in "..WIKIDATA_DIR..")")

   local dest_dir = WIKIDATA_DIR
   if STORAGE_MODULE == nil or STORAGE_MODULE == "wikifilestorage" then   
      make_versium_directory(dest_dir)
   else
      make_luasvnstorage_directory(dest_dir)
   end

   print("2.2 Copy the cgi executable to the local directory")
   copy(KEPLER_DIR.."/bin/cgi", "sputnik.cgi") -- copy the cgi executable to current dir
   print("2.3 Copy all the files to the installation directory...")
   install("colors.lua")
   install("cosmo.lua")
   install("wikistorage.lua")
   install("wikifilestorage.lua")
   install("wikisvnstorage.lua")
   install("sputnik")
   curdir = lfs.currentdir()
   lfs.chdir("dependencies")
   for file in lfs.dir(".") do
      if is_installable(file) then
         install(file)
      end
   end
   lfs.chdir(curdir)
   copy("cgilua_sputnik.lua", KEPLER_WEB.."/cgilua_sputnik.lua")
   require("cosmo") -- by now we should have cosmo installed
   print("2.4 Generate the config file and write it to")
   config_file_location = KEPLER_LUA.."/sputnik/config.lua"
   print("    "..config_file_location)
   make_config_file(config_file_location)
   print("2.5 Replace "..KEPLER_CONF.."/cgilua/config.lua")
   print("    with our version")
   copy("cgilua.config.lua", KEPLER_CONF.."/cgilua/config.lua")
end

function install(path)
   copy(path, KEPLER_LUA.."/"..path)
end

function copy(source, dest)
   if lfs.attributes(source).mode == "directory" then
      lfs.mkdir(dest)
      for child in lfs.dir(source) do
         if is_installable(child) then
            copy(source.."/"..child, dest.."/"..child)
         end
      end
   else
      local f = io.open(source)
      local data = f:read("*all*")
      f:close()
      f,e = io.open(dest, "w")
      if e then
         print(dest)
         error(e)
      end
      f:write(data)
      f:close()
   end
end

function is_installable(filename) 
   return filename and filename:len() > 0 and filename:sub(1,1)~="."
end
------------------------------------------------------
config_template = [[
module(..., package.seeall)
WIKI_DATA_DIR    = "$wikidata"
BASE_URL         = "$base_url"
SEARCH_API_KEY   = nil -- replace with your google search api 
SCRIPT_PATH      = "$script_path"
SECRET_CODE      = "$secret_code"
CONFIG_PAGE_NAME = "_config"
PASS_PAGE_NAME   = "_passwords"
VERSION          = "0.3.0-beta-from-svn"
]]
------------------------------------------------------

function make_config_file(dest)
   math.randomseed(os.time())
   local code = ""
   for i=1,20 do 
      code = code .. string.format("%03d", 1000*math.random())
   end
   local config = cosmo.fill(config_template, {  
                              script_path = KEPLER_WEB.."/cgilua_sputnik.lua",
                              wikidata    = WIKIDATA_DIR,
                              base_url    = BASE_URL,
                              secret_code = code 
                         }
   )
   f,e = io.open(dest, "w")
   if e then
      print("Error opening "..dest)
      error(e)
   end
   f:write(config)
   f:close()
end

function make_versium_directory(dest_dir)
   lfs.mkdir(dest_dir)
   require"versium"
   local v = versium.open{ storage = "versium.storage.simple", params = {dir=dest_dir} }
   
   local source_dir = "vdata/"
   for page_name in lfs.dir(source_dir) do
      if page_name:len() > 2 and (not (page_name:sub(1,1) == ".")) then
         print(page_name)
         -- read the source file --
         local f = io.open(source_dir .. page_name)
         if not f then
            error("Can't read file " .. source_dir .. page_name)
         end
         local page_content = f:read("*all")
         f:close()
         -- save it in versium --
         v:save_version(page_name, page_content, "Sputnik", "the default version")
      end
   end
end

install_all()

